package com.leet.bfs;

import java.util.*;

public class JuChiCengCI {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<>();

        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        int level = 1;
        while (!q.isEmpty()){
            int size = q.size();
            List<Integer> tmp = new ArrayList<>();
            for (int i = 0 ; i < size; i++){
                TreeNode head = q.poll();
                if(head != null) tmp.add(head.val);
                if(head.left!=null) q.offer(head.left);
                if(head.right!=null) q.offer(head.right);
            }
            if(level % 2 == 0) Collections.reverse(tmp);
            ans.add(tmp);
            level++;
        }
        return ans;
    }
}
